$(function() {
	platformId = "";// 当前页面所使用的平台id
	getPlatformId();// 
	getNowFormatDate();//插入时间戳到ATP页面
	//数据初始值
	getLocalStorageInit();
	//监听数据变化，存储本地数据
	addListenerEventFun();
	addNumberKeyboard();
});

tests=function(moudle){ // ATP页面数据发送功能
 	var inputs=$("#top input");//拿到所有的输入框
 	var selects=$("#top select");//拿到所有的下拉框
	if(moudle=="start"){//点击的是开始和终止按钮
		console.log("开始测试");
		getNowFormatDate();//插入时间戳到ATP页面
		var str=getDate(inputs,selects);
		ajaxSendATPSignal(str,1);
	}
	else if(moudle=="stop"){
		console.log("停止测试");
		getNowFormatDate();//插入时间戳到ATP页面
		var str=getDate(inputs,selects);
		ajaxSendATPSignal(str,3);
	}
	else if(moudle=="updated"){
		console.log("更新数据");
		getNowFormatDate();//插入时间戳到ATP页面
		var str=getDate(inputs,selects);
		console.log(str);
		ajaxSendATPSignal(str,2);
	}	
 };

 function getDate(inputs,selects){
		var str='{';
		for(var i=0;i<inputs.length;i++){//获取Input的键值
	    	var input = $(inputs[i]);
	    	var key =input.attr("name");
	   		var value =input.val();
	    	str += key+':"'+value+'",';
	    }
	    for(var i=0;i<selects.length;i++){//获取select的键值
	    	var select = $(selects[i]);
	    	var key =select.attr("name");
	   		var value =select.find("option:selected").attr("signalValue"); 
	   		if(selects.length-1==i){
				str += key+':"'+value+'"}';
	    	}else{
	    		str += key+':"'+value+'",';
	    	}
	    }
	    return str;
	}

function getPlatformId() {// 从链接路径中截取平台ID
	var url = location.search; // 获取url中"?"符后的字串
	path = location.host;
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		platformId = strs[3].split("=")[1];
	}
}

//获取当前的时间插入到ATP页面中的标签中展示出来
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var spance =" ";
    var year =date.getFullYear();//年
    var month = date.getMonth() + 1;//月
    var strDate = date.getDate();//日
    var hours =date.getHours();//时
    var minutes =date.getMinutes();//分
    var Seconds =date.getSeconds();//秒
    var Milliseconds= date.getMilliseconds();//毫秒
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if(hours >= 0 && hours <= 9){
    	hours = "0" + hours;
    }
    if(minutes >= 0 && minutes <= 9){
    	minutes = "0" + minutes;
    }
    if(Seconds >= 0 && Seconds <= 9){
    	Seconds = "0" + Seconds;
    }
    var currentdate = year+ seperator1 + month + seperator1 + strDate;//年月日
    var currentdates = hours+seperator2+minutes+seperator2+Seconds+spance+Milliseconds;//时分秒毫秒
   	$("#day").val(currentdate);
		$("#tim").val(currentdates);
}

function ajaxSendATPSignal(str,sendSignal) {//外围设备信息发送功能
	addLoading();
	$.ajax({
		"url" : "../../../atp/ajaxSendATPSignal",
		"type" : "post",
		"data" : {
			"sendSignal" : sendSignal,
			"platformId" : platformId,
			"str" : str
		},
		"success" : function(result) {
			removeLoading();
			console.log(result);
			if (result.flag == 0) {
				var bean = result.content;
				beanToStr(bean);
				console.log("发送成功");
				book("发送成功");
				
			} else {
				var content=result.errorInfo;
				book(content);
			}
		},
		"error" : function() {
			removeLoading();
			var content="手工测试连接错误！";
			book(content);
		}
	});
}

function beanToStr(bean){
	 if(bean.lkjTime!=null){
		 $("#lkjTime").text(bean.lkjTime);
	 }
	 if(bean.trainNum!=null){
		 $("#trainNumber").text(bean.trainNum);
	 }
	 if(bean.driverId!=null){
		 $("#driver").text(bean.driverId);
	 }
	 if(bean.controlCode!=null){
		 $("#commands").text(bean.controlCode);
	 }
}

function addListenerEventFun(){
	//监听数据变化，存储本地数据
	$("input").on('input',function(e)  
	{  
		switch (e.target.name){
			case 'TrainSpeed':
				localStorage.setItem('APTTrainSpeed', e.target.value);
				break;
			case 'LimitSpeed':
				localStorage.setItem('APTLimitSpeed', e.target.value);
				break;
			case 'TargetSpeed':
				localStorage.setItem('APTTargetSpeed', e.target.value);
				break;
				
			//列车室信息本地数据存储
			case 'TrackNo':
				localStorage.setItem('APTTrackNo', e.target.value);
				break;
				
			//列车位置本地数据存储
			case 'Distance':
				localStorage.setItem('APTDistance', e.target.value);
				break;
			case 'BaliseC':
				localStorage.setItem('APTBaliseC', e.target.value);
				break;
			case 'BaliseBG':
				localStorage.setItem('APTBaliseBG', e.target.value);
				break;
			default:
				break;
		}
	}); 
	
	//监听数据变化，存储本地数据
	$("select").on('change',function(e){
		switch (e.target.name){
			//列车控制模式本地数据存储
			case 'CTCSLevel':
				localStorage.setItem('APTCTCSLevel', e.target.selectedIndex);
				break;
			case 'ControlMode':
				localStorage.setItem('APTControlMode', e.target.selectedIndex);
				break;
			//列车室信息本地数据存储
			case 'Signal':
				localStorage.setItem('APTSignal', e.target.selectedIndex);
				break;
			case 'SpeedSignal':
				localStorage.setItem('APTSpeedSignal', e.target.selectedIndex);
				break;
				
			//列车位置本地数据存储
			case 'DistanceScale':
				localStorage.setItem('APTDistanceScale', e.target.selectedIndex);
				break;
			case 'TrainDirection1':
				localStorage.setItem('APTTrainDirection1', e.target.selectedIndex);
				break;
			case 'TrainDirection2':
				localStorage.setItem('APTTrainDirection2', e.target.selectedIndex);
				break;
			default:
				break;
		}
	});
}

//读取存储数据
function getLocalStorageInit(){
	//列车速度本地数据读取
	if(localStorage.getItem('APTTrainSpeed')){
		$("input[name='TrainSpeed']").val(localStorage.getItem('APTTrainSpeed'));
	}
	if(localStorage.getItem('APTLimitSpeed')){
		$("input[name='LimitSpeed']").val(localStorage.getItem('APTLimitSpeed'));
	}
	
	if(localStorage.getItem('APTTargetSpeed')){
		$("input[name='TargetSpeed']").val(localStorage.getItem('APTTargetSpeed'));
	}
	
	//列车控制模式本地数据读取
	if(localStorage.getItem('APTCTCSLevel')){
		$("select[name = 'CTCSLevel']").get(0).selectedIndex = localStorage.getItem('APTCTCSLevel');
	}
	if(localStorage.getItem('APTControlMode')){
		$("select[name = 'ControlMode']").get(0).selectedIndex = localStorage.getItem('APTControlMode');
	}
	
	//列车室信息本地数据读取
	if(localStorage.getItem('APTSignal')){
		$("select[name = 'Signal']").get(0).selectedIndex = localStorage.getItem('APTSignal');
	}
	if(localStorage.getItem('APTSpeedSignal')){
		$("select[name = 'SpeedSignal']").get(0).selectedIndex = localStorage.getItem('APTSpeedSignal');
	}
	if(localStorage.getItem('APTTrackNo')){
		$("input[name='TrackNo']").val(localStorage.getItem('APTTrackNo'));
	}
	
	//列车位置本地数据存储
	if(localStorage.getItem('APTDistanceScale')){
		$("select[name = 'DistanceScale']").get(0).selectedIndex = localStorage.getItem('APTDistanceScale');
	}
	if(localStorage.getItem('APTTrainDirection1')){
		$("select[name = 'TrainDirection1']").get(0).selectedIndex = localStorage.getItem('APTTrainDirection1');
	}
	if(localStorage.getItem('APTTrainDirection2')){
		$("select[name = 'TrainDirection2']").get(0).selectedIndex = localStorage.getItem('APTTrainDirection2');
	}
	if(localStorage.getItem('APTDistance')){
		$("input[name='Distance']").val(localStorage.getItem('APTDistance'));
	}
	if(localStorage.getItem('APTBaliseC')){
		$("input[name='BaliseC']").val(localStorage.getItem('APTBaliseC'));
	}
	if(localStorage.getItem('APTBaliseBG')){
		$("input[name='BaliseBG']").val(localStorage.getItem('APTBaliseBG'));
	}
}

//添加数字软键盘
function addNumberKeyboard(){
	if($(".numkeyboard").length>0){
		$(".numkeyboard").numkeyboard({
			keyboardRadix: 600, //键盘大小基数
			mainbackground: '#313439', //主背景色
			menubackground: '#313439', //头背景色
			exitbackground: '#4376A0', //关闭按钮背景色
			buttonbackground: 'transparent', //键盘背景色
			clickeve: true, //是否绑定元素click事件
		});
	}
}
